//二进制求和
//测试链接 https://leetcode.cn/problems/add-binary/
public class AddBinary {
    public String addBinary(String a, String b) {
        StringBuilder ret = new StringBuilder();
        int sum = 0; //表示进位
        int lena = a.length(), lenb = b.length();
        int i = lena - 1, j = lenb - 1;

        while(i >=0 || j >=0 || sum != 0){
            int num1 = 0, num2 = 0;
            if(i >= 0){
                num1 = a.charAt(i--) - '0';
            }
            if(j >= 0){
                num2 = b.charAt(j--) - '0';
            }

            sum += (num1 + num2);
            int temp = sum % 2; //当前位置相加后的数
            ret.append(String.valueOf(temp));

            sum /= 2;
        }
        return ret.reverse().toString();
    }
}
